package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.basics.exceptions.ThrowPageException;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.WMenus;
import com.sxbbc.common.core.entity.jz.WRoles;
import com.sxbbc.common.core.mapper.jz.WMenusMapper;
import com.sxbbc.common.core.mapper.jz.WRolesMapper;
import com.sxbbc.common.core.service.IWRolesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 仓库角色表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-07-01
 */
@Service
public class WRolesServiceImpl extends ServiceImpl<WRolesMapper, WRoles> implements IWRolesService {

    @Autowired
    private WMenusMapper wMenusMapper;

    @Override
    public List<WMenus> queryMenusByRoleId(Integer id) {
        String menuIds = "";
        if (id != null) {
            WRoles role = super.getById(id);
            if (role == null) {
                throw new ThrowPageException("未找到角色信息");
            }
            menuIds = "," + role.getMenuIds() + ",";
        }
        QueryWrapper<WMenus> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StaticUtils.STATUS_YES);
        wrapper.orderByAsc("id,type");
        List<WMenus> list = wMenusMapper.selectList(wrapper);
        for (WMenus l : list) {
            if (StringUtils.isNotBlank(menuIds)) {
                String m = "," + l.getId() + ",";
                if (menuIds.indexOf(m) > -1) {
                    l.setCheck("checked");
                }
            }
        }
        return list;
    }
}
